package com.zhkj.framework.controller;

import com.zhkj.framework.result.ResultVO;
import com.zhkj.framework.domain.User;
import com.zhkj.framework.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @Auther: zhangy
 * @Date: 2018/10/12 09:39
 * @Description:  SwaggerAPI接口文档自动生成器
 * 第一步：pom中引入springfox-swagger2和springfox-swagger-ui
 * 第二步：与controller包同级下创建配置文件swagger2.java
 * 第三步：controller文件中加入swagger相关注解
 * 第四步：在启动类加入@EnableSwagger2，否则会出现无法进入的问题
 */
@RestController
//api接口名称设置
@Api(value = "UserController  |  用户管理接口控制器")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("test")
    public String sayFramework(){
        return "This is a Framework!";
    }

    @GetMapping("user")
    //接口功能的描述
    @ApiOperation(value = "查询所有用户列表",notes = "返回全部用户数据")
    //@ApiImplicitParam(paramType = "query",name = "id",value = "用户编号")
    public List<User> selectaa(){
        return userService.finaUsers();
    }

    //api带参数的接口
    @GetMapping("phone")
    @ApiOperation(value = "通过电话号码查询用户", notes = "返回一个用户")
    @ApiImplicitParam(paramType = "query",name = "phone",value = "用户电话号码")
    public User selectUser(@ApiParam(name ="phone",value = "用户电话号码",required = true) @RequestParam("phone") String phone){

        return userService.findUser(phone);
    }

    //api带URL参数的接口
    @PostMapping("name/{name}")
    @ApiOperation(value = "删除用户",notes = "通过用户名删除用户")
    @ApiImplicitParam(paramType = "query",name = "name",value = "用户姓名")
    public String deleteUss(@ApiParam(name ="name", value = "用户姓名",required = true)  @PathVariable("name") String name){

        try{
            int no = userService.deleteUser(name);
            return "用户" + name + "删除成功";
        } catch (Exception e){
            return "用户删除不成功！";
        }
    }


    //新增用户
    @PostMapping("new")
    @ApiOperation(httpMethod = "POST",  value = "新增用户",notes = "新增用户")
    @ApiModelProperty(name = "用户")
    public ResultVO newUser(@RequestBody @ApiParam(name = "用户",value = "用户")   User user){

//         System.out.println(JSON.toJSONString(user));
//         return user;

        return userService.newUser(user);
    }


}
